package com.jspx.txweb.interceptor;

import com.jspx.boot.sign.LanguageRes;
import org.slf4j.Logger;
import com.jspx.boot.environment.Environment;
import org.slf4j.LoggerFactory;
import com.jspx.txweb.Action;
import com.jspx.txweb.ActionInvocation;
import com.jspx.txweb.support.ActionSupport;
import com.jspx.txweb.util.RequestUtil;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by yuan on 2015/7/18 0018.
 * 盗链拦截器 防止拦截
 *
 */
public class PiratedInterceptor extends InterceptorSupport {
    private static final Logger log = LoggerFactory.getLogger(PiratedInterceptor.class);
    public void init() {

    }

    public String intercept(ActionInvocation actionInvocation) throws Exception {
        //这里是不需要验证的action
        Action action = actionInvocation.getActionProxy().getAction();
        HttpServletRequest request = action.getRequest();
        if (language == null) return ActionSupport.ERROR;
        if (request!=null&& RequestUtil.isPirated(request))
        {
            action.addFieldInfo(Environment.warningInfo, language.getLang(LanguageRes.notAllowedPiratedLink));
            //action.setResult(language.getLang(LanguageRes.notAllowedPiratedLink));
            return ActionSupport.UNTITLED;
        }
        return actionInvocation.invoke();
        //也可以 return Action.ERROR; 终止action的运行
    }

    public void destroy() {

    }
}